package com.timing.finder.framework;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 应用上下文
 *
 * @author JingGe(* ^ ▽ ^ *)
 * @date 2023-03-08 13:58
 * @email 1158055613@qq.com
 */
public class TimingFinderApplicationContext {
    private final InheritableThreadLocal<TimingFinderApplication<? extends TimeSeriesData>> currentApplication = new InheritableThreadLocal<>();
    private final Map<String, TimingFinderApplication<? extends TimeSeriesData>> applicationContainer = new ConcurrentHashMap<>();

    private TimingFinderApplicationContext() {
    }

    public static TimingFinderApplicationContext getInstance() {
        return InstanceHolder.CONTEXT;
    }

    void init(TimingFinderApplication<? extends TimeSeriesData> application) {
        this.currentApplication.set(application);
        this.applicationContainer.put(application.getApplicationName(), application);
    }

    public TimingFinderApplication<? extends TimeSeriesData> currentApplication() {
        return currentApplication.get();
    }

    public TimingFinderApplication<? extends TimeSeriesData> getCurrentApplicationByName(String name) {
        return applicationContainer.get(name);
    }

    static class InstanceHolder {
        private static final TimingFinderApplicationContext CONTEXT = new TimingFinderApplicationContext();
    }
}
